﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MaterialDemo.IService.IWorkFlow;
using MaterialDemo.Model.WorkFlowDTO;
using MaterialDemo.MVC.Filters;
using MaterialDemo.Utility;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace MaterialDemo.MVC.Controllers
{
    [MySession]
    public class WorkFlowController : Controller
    {

        #region  构造函数注入
        private readonly ILogger<WorkFlowController> _logger;
        private readonly IWorkFlowService _workFlowService;


        public WorkFlowController(ILogger<WorkFlowController> logger, IWorkFlowService workFlowService)
        {

            _logger = logger;
            _workFlowService = workFlowService;


        }
        #endregion


        #region 页面



        /// <summary>
        /// 查看实例步骤
        /// </summary>
        /// <param name="instanceId"></param>
        /// <returns></returns>
        public IActionResult WorkFlowStepIndex(string instanceId)
        {
            ViewBag.list = _workFlowService.LoadStepEntities(instanceId);
            return View();
        }

        /// <summary>
        /// 查看耗材领取流程图
        /// </summary>
        /// <param name="instanceId"></param>
        /// <returns></returns>
        public IActionResult WorkFlowGoods_ConsumableIndex()
        {

            return View();
        }

        /// <summary>
        /// 查看非耗材领取流程图
        /// </summary>
        /// <param name="instanceId"></param>
        /// <returns></returns>
        public IActionResult WorkFlowGoods_EquipmentIndex()
        {

            return View();
        }

        #endregion


        #region 非页面

        #region 耗材部分
        /// <summary>
        /// 获取用户领取耗材记录的工作流实例
        /// </summary>
        /// <returns></returns>
        public string GetConsumableApplyByUser(int page, int limit)
        {
            string userId = HttpContext.Session.GetString("UserId");
            int totalCount = 0;
            List<WorkFlowInstanceOutput> outputs = _workFlowService.LoadPageEntitiesByUserId(page, limit, out totalCount, userId,true);
            DataResult<List<WorkFlowInstanceOutput>> dataResult = new DataResult<List<WorkFlowInstanceOutput>>
            {
                code = 0,
                msg = "",
                count = totalCount,
                data = outputs,
            };
            return JsonNetHelper.SerializetoJson(dataResult);
        }

        /// <summary>
        /// 获取需要审核的工作流实例
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public string GetConsumableApplyByReviewer(int page, int limit)
        {
            string userId = HttpContext.Session.GetString("UserId");
            int totalCount = 0;
            List<WorkFlowInstanceOutput> outputs = _workFlowService.LoadPageEntitiesByReviewer(page, limit, out totalCount, userId,true);
            DataResult<List<WorkFlowInstanceOutput>> dataResult = new DataResult<List<WorkFlowInstanceOutput>>
            {
                code = 0,
                msg = "",
                count = totalCount,
                data = outputs,
            };
            return JsonNetHelper.SerializetoJson(dataResult);
        }

        /// <summary>
        /// 获取需要出库耗材的工作流实例
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="selectInfo"></param>
        /// <returns></returns>
        public string GetConsumableOutput(int page, int limit, string selectInfo)
        {
            int totalCount = 0;
            List<WorkFlowInstanceOutput> outputs = _workFlowService.LoadPageEntitiesByKeeper(page, limit, out totalCount, selectInfo,true);
            DataResult<List<WorkFlowInstanceOutput>> dataResult = new DataResult<List<WorkFlowInstanceOutput>>
            {
                code = 0,
                msg = "",
                count = totalCount,
                data = outputs,
            };
            return JsonNetHelper.SerializetoJson(dataResult);
        }

        /// <summary>
        /// 提交耗材领取申请
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public string ApplyConsumable(WorkFlowInstanceInput input)
        {
            input.UserId = HttpContext.Session.GetString("UserId");
            if (_workFlowService.ApplyConsumable(input))
            {
                return "ok";
            }
            else
            {
                return "no";
            }
        }

        /// <summary>
        /// 撤销耗材领取申请
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public string RevokeConsumable(int Id)
        {
            if (_workFlowService.RevokeConsumable(Id))
            {
                return "ok";
            }
            else
            {
                return "no";
            }
        }

        /// <summary>
        /// 主管审核耗材领取
        /// </summary>
        /// <param name="instanceId"></param>
        /// <param name="isPass"></param>
        /// <param name="reason"></param>
        /// <returns></returns>
        public string ReviewConsumable(string instanceId, bool isPass, string reason)
        {
            string reviwerId = HttpContext.Session.GetString("UserId");
            if (_workFlowService.ReviewConsumable(instanceId, reviwerId, isPass, reason))
            {
                return "ok";
            }
            else
            {
                return "no";
            }
        }

        /// <summary>
        /// 耗材批量出库
        /// </summary>
        /// <param name="arrStr"></param>
        /// <returns></returns>
        public string OutpuConsumable(string arrStr)
        {
            string userId = HttpContext.Session.GetString("UserId");
            if (_workFlowService.OutpuConsumable(arrStr, userId))
            {
                return "ok";
            }
            else
            {
                return "no";
            }
        }
        #endregion

        #region 非耗材部分
        /// <summary>
        /// 获取用户领取非耗材记录的工作流实例
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public string GetEquipmentApplyByUser(int page, int limit)
        {
            string userId = HttpContext.Session.GetString("UserId");
            int totalCount = 0;
            List<WorkFlowInstanceOutput> outputs = _workFlowService.LoadPageEntitiesByUserId(page, limit, out totalCount, userId, false);
            DataResult<List<WorkFlowInstanceOutput>> dataResult = new DataResult<List<WorkFlowInstanceOutput>>
            {
                code = 0,
                msg = "",
                count = totalCount,
                data = outputs,
            };
            return JsonNetHelper.SerializetoJson(dataResult);
        }

        /// <summary>
        /// 获取需要审核的工作流实例
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public string GetEquipmentApplyByReviewer(int page, int limit)
        {
            string userId = HttpContext.Session.GetString("UserId");
            int totalCount = 0;
            List<WorkFlowInstanceOutput> outputs = _workFlowService.LoadPageEntitiesByReviewer(page, limit, out totalCount, userId, false);
            DataResult<List<WorkFlowInstanceOutput>> dataResult = new DataResult<List<WorkFlowInstanceOutput>>
            {
                code = 0,
                msg = "",
                count = totalCount,
                data = outputs,
            };
            return JsonNetHelper.SerializetoJson(dataResult);
        }

        /// <summary>
        /// 获取需要出库的非耗材的工作流实例
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="selectInfo"></param>
        /// <returns></returns>
        public string GetEquipmentOutput(int page, int limit, string selectInfo)
        {
            int totalCount = 0;
            List<WorkFlowInstanceOutput> outputs = _workFlowService.LoadPageEntitiesByKeeper(page, limit, out totalCount, selectInfo, false);
            DataResult<List<WorkFlowInstanceOutput>> dataResult = new DataResult<List<WorkFlowInstanceOutput>>
            {
                code = 0,
                msg = "",
                count = totalCount,
                data = outputs,
            };
            return JsonNetHelper.SerializetoJson(dataResult);
        }

        /// <summary>
        /// 分页获取已借出的非耗材工作流实例
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="selectInfo"></param>
        /// <returns></returns>
        public string GetEquipmentIsLent(int page, int limit, string selectInfo)
        {
            int totalCount = 0;
            List<WorkFlowInstanceOutput> outputs = _workFlowService.LoadPageIsLentEquipment(page, limit, out totalCount, selectInfo);
            DataResult<List<WorkFlowInstanceOutput>> dataResult = new DataResult<List<WorkFlowInstanceOutput>>
            {
                code = 0,
                msg = "",
                count = totalCount,
                data = outputs,
            };
            return JsonNetHelper.SerializetoJson(dataResult);
        }

        /// <summary>
        /// 提交非耗材领取申请
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public string ApplyEquipment(WorkFlowInstanceInput input)
        {
            input.UserId = HttpContext.Session.GetString("UserId");
            if (_workFlowService.ApplyEquipment(input))
            {
                return "ok";
            }
            else
            {
                return "no";
            }
        }

        /// <summary>
        /// 撤销非耗材领取申请
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public string RevokeEquipment(int Id)
        {
            if (_workFlowService.RevokeEquipment(Id))
            {
                return "ok";
            }
            else
            {
                return "no";
            }
        }

        /// <summary>
        /// 主管审核非耗材领取
        /// </summary>
        /// <param name="instanceId"></param>
        /// <param name="isPass"></param>
        /// <param name="reason"></param>
        /// <returns></returns>
        public string ReviewEquipment(string instanceId, bool isPass, string reason)
        {
            string reviwerId = HttpContext.Session.GetString("UserId");
            if (_workFlowService.ReviewEquipment(instanceId, reviwerId, isPass, reason))
            {
                return "ok";
            }
            else
            {
                return "no";
            }
        }

        /// <summary>
        /// 非耗材批量出库
        /// </summary>
        /// <param name="arrStr"></param>
        /// <returns></returns>
        public string OutputEquipment(string arrStr)
        {
            string userId = HttpContext.Session.GetString("UserId");
            if (_workFlowService.OutputEquipment(arrStr, userId))
            {
                return "ok";
            }
            else
            {
                return "no";
            }
        }

        /// <summary>
        /// 归还非耗材
        /// </summary>
        /// <returns></returns>
        public string ReturnEquipment(string arrStr)
        {
            string userId = HttpContext.Session.GetString("UserId");
            if (_workFlowService.ReturnEquipment(arrStr, userId))
            {
                return "ok";
            }
            else
            {
                return "no";
            }
        }
        #endregion

        #endregion
    }
}